/*
 * Copyright (c) 2008, Spagettikod | Stockholm
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the Spagettikod | Stockholm nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.spagettikod.t437;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.spagettikod.SystemUtil;
import com.xerox.amazonws.sdb.SimpleDB;

public class T437
{

	public static final String APP_NAME = "T-437";

	public static final String VERSION = "0.1";

	private static AWSJTree tree;

	private static JFrame frame;

	private static SimpleDB simpleDB = null;

	private static JScrollPane rightPane;

	private static Configurator config = new Configurator();

	private JSplitPane splitPane;

	private JScrollPane leftPane;

	public T437()
	{
		frame = new JFrame(APP_NAME);
		frame.setBounds(config.getMainWindowXPosition(), config.getMainWindowYPosition(), config.getMainWindowWidth(),
				config.getMainWindowHeight());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Add shutdown hook which saves user settings.
		Runtime.getRuntime().addShutdownHook(new Thread()
		{

			@Override
			public void run()
			{
				config.setMainWindowXPosition(frame.getX());
				config.setMainWindowYPosition(frame.getY());
				config.setMainWindowHeight(frame.getHeight());
				config.setMainWindowWidth(frame.getWidth());
				config.save();
			}

		});

		setLookAndFeel();

		if (!config.isAccessConfigured())
		{
			new AccessKeyDialog(frame, config);
		}

		if (!config.isAccessConfigured())
		{
			System.exit(-1);
		}

		createAndShowGUI();

	}

	private void setLookAndFeel()
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (UnsupportedLookAndFeelException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void createAndShowGUI()
	{
		tree = new AWSJTree(this);
		leftPane = new JScrollPane(tree);
		rightPane = new JScrollPane();

		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);
		splitPane.setResizeWeight(0.2);

		frame.getContentPane().add(splitPane);

		/*JMenuBar menubar = new JMenuBar();
		JMenu menu = new JMenu("File");
		menubar.add(menu);

		frame.setJMenuBar(menubar);*/

		// Display the window.
		frame.setPreferredSize(new Dimension(640, 480));
		if (config.getMainWindowWidth() == 0)
		{
			frame.pack();
		}
		frame.setVisible(true);
	}

	public void setRightPane(Component comp)
	{
		splitPane.setRightComponent(comp);
	}

	public SimpleDB getSimpleDB()
	{
		if (simpleDB == null)
		{
			simpleDB = new SimpleDB(config.getAccessKey(), config.getSecretKey());
		}
		return simpleDB;
	}

	public static void main(String[] args)
	{
		if (SystemUtil.isMacOSX())
		{
			System.setProperty("apple.laf.useScreenMenuBar", "true");
			System.setProperty("com.apple.mrj.application.growbox.intrudes", "false");
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", APP_NAME);
		}
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				new T437();
			}
		});
	}
}
